package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdWildBox;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdWildBoxDaoImpl extends TempDataObjectDao<StrongholdWildBox> {
    public static String getStrongholdWildBoxSql = "select * from t_c_activity_stronghold_box where activityId=? and groupId=?";

    public Map<Integer, StrongholdWildBox> getStrongholdWildBox(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdWildBox> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getStrongholdWildBoxSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdWildBox strongholdWildBox = parse(rs);
                dataMap.put(strongholdWildBox.getBoxId(), strongholdWildBox);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdWildBoxSql = "update t_c_activity_stronghold_box set wildId=?,boxType=?,serverId=?,userId=?,pickUpTime=?,openTime=?,reward = ?,beSearched = ? where activityId=? and boxId=? and groupId=?";

    @Override
    protected boolean updateData(StrongholdWildBox strongholdWildBox) {
        strongholdWildBox.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getWildId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildBox.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildBox.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildBox.getPickUpTime()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildBox.getOpenTime()));
        para.put(i++, new DbParameter(Types.NVARCHAR, strongholdWildBox.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdWildBox.isBeSearched()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getBoxId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getGroupId()));
        if (getDb().executeNoneQuery(updateStrongholdWildBoxSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdWildBoxSql = "insert into t_c_activity_stronghold_box(activityId,groupId,boxId,wildId,boxType,serverId,userId,pickUpTime,openTime,reward,beSearched) values (?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(StrongholdWildBox strongholdWildBox) {
        strongholdWildBox.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getBoxId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getWildId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildBox.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildBox.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildBox.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildBox.getPickUpTime()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildBox.getOpenTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdWildBox.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdWildBox.isBeSearched()));
        if (getDb().executeNoneQuery(insertStrongholdWildBoxSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdWildBox parseData(ResultSet rs) throws SQLException {
        StrongholdWildBox strongholdWildBox = new StrongholdWildBox();
        strongholdWildBox.setActivityId(rs.getInt("activityId"));
        strongholdWildBox.setGroupId(rs.getInt("groupId"));
        strongholdWildBox.setBoxId(rs.getInt("boxId"));
        strongholdWildBox.setWildId(rs.getInt("wildId"));
        strongholdWildBox.setType(rs.getInt("boxType"));
        strongholdWildBox.setServerId(rs.getLong("serverId"));
        strongholdWildBox.setUserId(rs.getLong("userId"));
        strongholdWildBox.setPickUpTime(rs.getLong("pickUpTime"));
        strongholdWildBox.setOpenTime(rs.getLong("openTime"));
        strongholdWildBox.setReward(rs.getString("reward"));
        strongholdWildBox.setBeSearched(rs.getBoolean("beSearched"));
        return strongholdWildBox;
    }
}
